package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.UserChangeMobileFromDto;
import com.dog.basics.bean.model.common.DogUser;
import com.dog.basics.bean.model.common.DogUserChangeMobile;
import com.dog.basics.bean.vo.admin.common.UserChangeMobileVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogUserChangeMobileMapper;
import com.dog.basics.dao.common.DogUserMapper;
import com.dog.basics.service.common.IDogUserChangeMobileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户手机修改变动变 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-05-27
 */
@Service
@AllArgsConstructor
public class DogUserChangeMobileServiceImpl extends ServiceImpl<DogUserChangeMobileMapper, DogUserChangeMobile> implements IDogUserChangeMobileService {
    private final DogUserMapper dogUserMapper;

    /**
     * 分页的条件公用类
     *
     * @param name
     * @param userIds
     */
    public List<Long> getuserIdsWhere(String name, List<Long> userIds) {
        List<Long> collect = dogUserMapper.selectList(Wrappers.<DogUser>lambdaQuery().like(DogUser::getNickName, name)).stream().map(e -> e.getUserId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            userIds = collect;
        } else {
            userIds.add(-2L);
        }
        return userIds;
    }

    /**
     * @Description: 分页用户手机修改变动
     * @Param: status
     * @return: IPage<UserChangeMobileVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<UserChangeMobileVo> getPageList(PageParam page, UserChangeMobileFromDto obj) {
        // 判断用户ID
        List<Long> userIds = CollUtil.newArrayList();
        if (StrUtil.isNotBlank(obj.getUserName())) {
            userIds = getuserIdsWhere(obj.getUserName(), userIds);
        }

        IPage iPage = this.page(page, new LambdaQueryWrapper<DogUserChangeMobile>()
                .eq(ObjectUtil.isNotNull(obj.getUserId()), DogUserChangeMobile::getUserId, obj.getUserId())
                .in(CollUtil.isNotEmpty(userIds), DogUserChangeMobile::getUserId, userIds)
                .like(StrUtil.isNotBlank(obj.getOldMobile()), DogUserChangeMobile::getOldMobile, obj.getOldMobile())
                .like(StrUtil.isNotBlank(obj.getNewMobile()), DogUserChangeMobile::getNewMobile, obj.getNewMobile())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogUserChangeMobile::getCreateTime));
        List<DogUserChangeMobile> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            UserChangeMobileVo newVo = new UserChangeMobileVo();
            BeanUtil.copyProperties(e, newVo);
            //查询用户
            DogUser dogUser = dogUserMapper.selectById(e.getUserId());
            newVo.setUserName(ObjectUtil.isNotNull(dogUser) ? dogUser.getNickName() : "");
            return newVo;
        }).collect(Collectors.toList()));
        return iPage;
    }

    /**
     * @Description: 删除用户手机修改变动
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(List<Long> ids) {
        // 判断是否为空
        Validator.validateTrue(CollUtil.isNotEmpty(ids), "主标识为空");
        // 删除意见信息
        return this.removeByIds(ids);
    }
}
